package com.wei.duowan.bbs.ui.activity;

import android.content.res.Resources;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.presenter.BasePresenter;
import com.wei.duowan.bbs.ui.fragment.BaseFragment;
import com.wei.duowan.bbs.ui.fragment.ForumsFragment;
import com.wei.duowan.bbs.ui.fragment.GiftFragment;
import com.wei.duowan.bbs.ui.fragment.MineFragment;
import com.wei.duowan.bbs.ui.fragment.MsgFragment;
import com.wei.duowan.bbs.ui.fragment.NewsFragment;

import butterknife.Bind;
import butterknife.OnClick;

public class MainActivity extends BaseActivity
{
    @Bind(R.id.img_home)
    ImageView mImgHome;
    @Bind(R.id.tv_home_)
    TextView mTvHome;
    @Bind(R.id.img_bbs)
    ImageView mImgBbs;
    @Bind(R.id.tv_bbs_)
    TextView mTvBbs;
    @Bind(R.id.img_gift)
    ImageView mImgGift;
    @Bind(R.id.tv_gift_)
    TextView mTvGift;
    @Bind(R.id.img_msg)
    ImageView mImgMsg;
    @Bind(R.id.tv_msg_)
    TextView mTvMsg;
    @Bind(R.id.img_mine)
    ImageView mImgMine;
    @Bind(R.id.tv_mine_)
    TextView mTvMine;
    @Bind(R.id.fl_advert)
    FrameLayout mFlAdvert;
    @Bind(R.id.img_advert)
    ImageView mImgAdvert;


    private FragmentManager mManager = null;
    private BaseFragment mHomeFragment, mForumsFragment,
            mGiftFragment, mMsgFragment, mMineFragment = null;

    @Override
    protected BasePresenter createPresenter()
    {
        return null;
    }

    @Override
    protected int getLayoutID()
    {
        return R.layout.activity_main;
    }

    @Override
    protected void initView()
    {
        mManager = getSupportFragmentManager();
        showFragment(1);
        setImgAdvertAnimation();
    }

    /*显示Fragment*/
    private void showFragment(int position)
    {
        FragmentTransaction ft = mManager.beginTransaction();
        hideFragment(ft);
        showTitle(position);
        switch (position)
        {
            case 0:
                if (mHomeFragment == null)
                {
                    mHomeFragment = new NewsFragment();
                    ft.add(R.id.layout_container, mHomeFragment);
                } else
                {
                    ft.show(mHomeFragment);
                }
                break;
            case 1:
                if (mForumsFragment == null)
                {
                    mForumsFragment = new ForumsFragment();
                    ft.add(R.id.layout_container, mForumsFragment);
                } else
                {
                    ft.show(mForumsFragment);
                }
                break;
            case 2:
                if (mGiftFragment == null)
                {
                    mGiftFragment = new GiftFragment();
                    ft.add(R.id.layout_container, mGiftFragment);
                } else
                {
                    ft.show(mGiftFragment);
                }
                break;
            case 3:
                if (mMsgFragment == null)
                {
                    mMsgFragment = new MsgFragment();
                    ft.add(R.id.layout_container, mMsgFragment);
                } else
                {
                    ft.show(mMsgFragment);
                }
                break;
            case 4:
                if (mMineFragment == null)
                {
                    mMineFragment = new MineFragment();
                    ft.add(R.id.layout_container, mMineFragment);
                } else
                {
                    ft.show(mMineFragment);
                }
                break;
        }
        ft.commit();
    }

    /*隐藏不需要显示的Fragment*/
    private void hideFragment(FragmentTransaction ft)
    {
        if (mHomeFragment != null)
        {
            ft.hide(mHomeFragment);
        }
        if (mForumsFragment != null)
        {
            ft.hide(mForumsFragment);
        }
        if (mGiftFragment != null)
        {
            ft.hide(mGiftFragment);
        }
        if (mMsgFragment != null)
        {
            ft.hide(mMsgFragment);
        }
        if (mMineFragment != null)
        {
            ft.hide(mMineFragment);
        }
    }

    /*显示底部的Icon和Title*/
    private void showTitle(int position)
    {
        Resources resource = App.getResource();
        mImgHome.setImageResource(R.mipmap.navi_tab_news_default);
        mTvHome.setTextColor(resource.getColor(R.color.color7d));

        mImgBbs.setImageResource(R.mipmap.navi_tab_bbs_default);
        mTvBbs.setTextColor(resource.getColor(R.color.color7d));

        mImgGift.setImageResource(R.mipmap.navi_tab_game_default);
        mTvGift.setTextColor(resource.getColor(R.color.color7d));

        mImgMsg.setImageResource(R.mipmap.navi_tab_message_default);
        mTvMsg.setTextColor(resource.getColor(R.color.color7d));

        mImgMine.setImageResource(R.mipmap.navi_tab_user_default);
        mTvMine.setTextColor(resource.getColor(R.color.color7d));
        switch (position)
        {
            case 0:
                mImgHome.setImageResource(R.mipmap.navi_tab_news_selected);
                mTvHome.setTextColor(resource.getColor(R.color.color));
                break;
            case 1:
                mImgBbs.setImageResource(R.mipmap.navi_tab_bbs_selected);
                mTvBbs.setTextColor(resource.getColor(R.color.color));
                break;
            case 2:
                mImgGift.setImageResource(R.mipmap.navi_tab_game_selected);
                mTvGift.setTextColor(resource.getColor(R.color.color));
                break;
            case 3:
                mImgMsg.setImageResource(R.mipmap.navi_tab_message_selected);
                mTvMsg.setTextColor(resource.getColor(R.color.color));
                break;
            case 4:
                mImgMine.setImageResource(R.mipmap.navi_tab_user_selected);
                mTvMine.setTextColor(resource.getColor(R.color.color));
                break;
        }
    }


    /*广告图动效*/
    private void setImgAdvertAnimation()
    {
        final TranslateAnimation left = new TranslateAnimation(0F, -30F, 0, 0);
        left.setDuration(400L);
        final TranslateAnimation right = new TranslateAnimation(-30F, 0, 0, 0);
        right.setDuration(400L);

        left.setAnimationListener(new Animation.AnimationListener()
        {
            @Override
            public void onAnimationStart(Animation animation)
            {

            }

            @Override
            public void onAnimationEnd(Animation animation)
            {
                if (right != null)
                {
                    mFlAdvert.startAnimation(right);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {

            }
        });

        right.setAnimationListener(new Animation.AnimationListener()
        {
            @Override
            public void onAnimationStart(Animation animation)
            {

            }

            @Override
            public void onAnimationEnd(Animation animation)
            {
                if (left != null)
                {
                    mFlAdvert.startAnimation(left);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {

            }
        });

        mFlAdvert.startAnimation(left);
    }


    @OnClick({R.id.tv_home, R.id.tv_bbs, R.id.tv_gift, R.id.tv_msg, R.id.tv_mine, R.id.img_advert, R.id.img_advert_})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.tv_home:
                showFragment(0);
                break;
            case R.id.tv_bbs:
                showFragment(1);
                break;
            case R.id.tv_gift:
                showFragment(2);
                break;
            case R.id.tv_msg:
                showFragment(3);
                break;
            case R.id.tv_mine:
                showFragment(4);
                break;
            case R.id.img_advert:

                break;
            case R.id.img_advert_:
                mFlAdvert.setVisibility(View.GONE);
                break;
        }
    }


}
